
package com.ifcar.app.activity.newspage;

import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.activity.NewsPage;
import com.ifcar.app.activity.HomePage;
import com.ifcar.app.activity.common.CommentFragment;
import com.ifcar.app.activity.common.CommentWithTitleFragment;
import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.fragment.NRTabFragment.TabManager.TabInfo;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.activity.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class NewsCommentFragment extends NRTabFragment {

    public static final String NEWS_FRAGMENT = "news_fragment";

    public static final String COMMENT_FRAGMENT = "commnet_fragment";

    public static final String PARAM_FROM_COMMENT = "from_comment";

    public static final String PARAM_FOR_COMMENT = "param_comment";

    public static final String PARAM_FOR_NEWS = "param_news";
    
    public static final String PARAM_FOR_GO_MAIN = "param_go_main";

    private String mLastTab;

    private int mLastTabIndex = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.news_comment_layout, container, false);
    }

    @Override
    public int getContainerId() {
        return R.id.realtabcontent;
    }

    @Override
    public void onTabChanged(String tabId) {
        super.onTabChanged(tabId);

        TabHost tabHost = getTabHost();

        int currentIndex = tabHost.getCurrentTab();
        String currentTab = tabHost.getCurrentTabTag();

        if (mLastTabIndex != -1) {

            if (mLastTabIndex > currentIndex) {
                Fragment f = getTabManager().getTabInfos().get(mLastTab).getFragment();
                if (f != null && f.getView() != null) {
                    f.getView().startAnimation(
                            AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out_fast));
                }
            } else if (currentIndex > mLastTabIndex) {
                Fragment f = getTabManager().getTabInfos().get(currentTab).getFragment();
                if (f != null && f.getView() != null) {
                    f.getView().startAnimation(
                            AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_fast));
                }

                f = getTabManager().getTabInfos().get(mLastTab).getFragment();
                if (f != null && f.getView() != null) {
                    f.getView().startAnimation(
                            AnimationUtils.loadAnimation(getActivity(), R.anim.remain));
                }
            }
        }

        mLastTabIndex = currentIndex;
        mLastTab = tabId;
    }

    public void goback() {
        gobackInternal(false);
    }
    
    private void gobackInternal(boolean fromBackKey) {
        TabHost tabHost = getTabHost();
        if (tabHost == null || tabHost.getCurrentTab() <= 0) {
            Activity activity = getActivity();
            
            Bundle args = getArguments();
            boolean gomain = args == null ? false : args.getBoolean(PARAM_FOR_GO_MAIN, false);
            if (gomain) {
                MyApplication appdata = getApplication();
                boolean isexit = appdata.isExit();

                if (isexit) {
                    Intent intent = new Intent(appdata, HomePage.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(intent);
                    appdata.setExit(false);
                }
                finishActivity(activity);
            } else if (!fromBackKey && activity instanceof NewsPage) {
                ((NewsPage)activity).finishAllNewsPage();
            } else {
                finishActivity(activity);
            }
            
            return;
        }
        tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
    }
    
    private void finishActivity(Activity activity) {
        if (activity instanceof NewsPage) {
            ((NewsPage)activity).finishNewsPage();
        } else {
            activity.finish();
        }        
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean handled = super.dispatchKeyEvent(event);
        if (!handled && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                gobackInternal(true);
            }
            return true;
        }
        return handled;
    }

    @Override
    protected void initTabhost(TabManager tabManager, TabHost tabHost) {
        Bundle args = getArguments();

        boolean fromComment = args == null ? false : args.getBoolean(PARAM_FROM_COMMENT, false);
        if (fromComment) {
            Bundle commentArgs = args == null ? null : args.getBundle(PARAM_FOR_COMMENT);
            if (commentArgs != null) {
                commentArgs.putBoolean(CommentWithTitleFragment.SHOW_GO_NEWS, true);
            }
            addSpec(tabManager, tabHost, COMMENT_FRAGMENT, CommentWithTitleFragment.class,
                    commentArgs);

            Bundle newsArgs = null;
            if (commentArgs != null) {
                newsArgs = new Bundle();
                newsArgs.putString(GetColumnsInfo.DOCID_TAG,
                        commentArgs.getString(CommentFragment.DOC_ID));
                newsArgs.putBoolean(NewsFragment.SHOW_GO_COMMENT, false);
            }

            addSpec(tabManager, tabHost, NEWS_FRAGMENT, NewsFragment.class, newsArgs);

        } else {
            Bundle newsArgs = args == null ? null : args.getBundle(PARAM_FOR_NEWS);
            if (newsArgs != null) {
                newsArgs.putBoolean(NewsFragment.SHOW_GO_COMMENT, true);
            }
            addSpec(tabManager, tabHost, NEWS_FRAGMENT, NewsFragment.class, newsArgs);

            Bundle commentArgs = new Bundle();
            commentArgs.putBoolean(CommentWithTitleFragment.SHOW_GO_NEWS, false);
            addSpec(tabManager, tabHost, COMMENT_FRAGMENT, CommentWithTitleFragment.class,
                    commentArgs);
        }
    }

    private void addSpec(TabManager tabManager, TabHost tabHost, String tag, Class clazz,
            Bundle bundle) {
        TabSpec spec = tabHost.newTabSpec(tag);
        View view = new View(getActivity());
        spec.setIndicator(view);

        TabInfo info = new TabInfo(tag, clazz, bundle);

        tabManager.addTab(spec, info);
    }
    
    public void dispatchNewsFragment(){
        String tag = getTabHost().getCurrentTabTag();
        if(NEWS_FRAGMENT.equals(tag)){
            ((NewsFragment)getTabManager().getTabInfo(NEWS_FRAGMENT).getFragment()).setCanPageTurn();
        }
    }
    
    public void showComment(Bundle args) {
        TabManager tabManager = getTabManager();
        if (tabManager == null) {
            return;
        }
        if (COMMENT_FRAGMENT.equals(getTabHost().getCurrentTabTag())) {
            return;
        }

        TabInfo info = tabManager.getTabInfos().get(COMMENT_FRAGMENT);
        if (info.getFragment() == null) {
            Bundle b = info.getArgs();
            if (info.getArgs() != null) {
                info.getArgs().putBundle(CommentFragment.PARAM_COMMENT, args);
            }
        } else {
            Bundle b = info.getFragment().getArguments();
            if (b != null) {
                b.putBundle(CommentFragment.PARAM_COMMENT, args);
            }
        }

        getTabHost().setCurrentTabByTag(COMMENT_FRAGMENT);
    }

    public void showNews() {
        TabManager tabManager = getTabManager();
        if (tabManager == null) {
            return;
        }
        if (NEWS_FRAGMENT.equals(getTabHost().getCurrentTabTag())) {
            return;
        }

        getTabHost().setCurrentTabByTag(NEWS_FRAGMENT);
    }
}
